package cn.com.ghy.android.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.TaskBean;
import cn.com.ghy.android.bean.TaskBeanParser;
import cn.com.ghy.android.bean.WorkReportBean;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:最新任务服务器
 * @author wuz  @date 2011-5-23 16:49:14
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class NoteOpeService {
	private String url = CommonStatic.URL_HEAD + "NoteOpe_Ar.asmx";
	private String methodName = "";
	private Map<String, String> params = null;

	private static final String CUSER = "cuser";
	private static final String TASKID = "taskid";
	private static final String TASKHEAD = "taskhead";
	private static final String NOTEHEAD = "notehead";

	public List<TaskBean> GetNewTask(LoginInfoBean loginInfo) throws Exception {
		methodName = "GetNewTask";
		params = new HashMap<String, String>();
		params.put(CUSER, loginInfo.getPernr());

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<TaskBean> list = TaskBeanParser.parseXMLForTaskList(in);
		in.close();
		CommonUtil.checkListNotNull(list);
		return list;
	}

	public List<TaskBean> GetTaskDetail(TaskBean taskBean) throws Exception {
		methodName = "GetTaskDetail";
		params = new HashMap<String, String>();
		params.put(TASKID, taskBean.getTaskId());

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<TaskBean> list = TaskBeanParser.parseXMLForTaskDetail(in);
		in.close();
		CommonUtil.checkListNull(list);
		return list;
	}

	public String SetTaskDeal(TaskBean taskBean) throws Exception {
		methodName = "SetTaskDeal";
		params = new HashMap<String, String>();

		String taskheadXML = "";
		StringWriter writer = new StringWriter();
		taskheadXML = TaskBeanParser.createXMLTaskhead(taskBean, writer);
		writer.close();
		taskheadXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(taskheadXML
				.getBytes()));

		params.put(TASKHEAD, taskheadXML);

		String result = HttpService.callWS(url, methodName, params);
		return result;
	}

	public String SetNote(WorkReportBean bean) throws Exception {
		methodName = "SetNote";
		params = new HashMap<String, String>();

		String noteheadXML = "";
		StringWriter writer = new StringWriter();
		noteheadXML = TaskBeanParser.createXMLNoteheadX(bean, writer);
		writer.close();
		noteheadXML = org.kobjects.base64.Base64.encode(CommonUtil.compressByteArray(noteheadXML
				.getBytes()));

		params.put(NOTEHEAD, noteheadXML);

		String result = HttpService.callWS(url, methodName, params);
		return result;
	}

	public String GetHasNewTask(String cuser) throws Exception {
		methodName = "GetHasNewTask";
		params = new HashMap<String, String>();
		params.put(CUSER, cuser);
		String result = null;
		result = HttpService.callWS(url, methodName, params);
		return result;
	}
}
